package com.dykj.apps.web.sysmanager.service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.dykj.apps.web.sysmanager.dao.IAuthManagerDAO;
import com.dykj.apps.web.sysmanager.dao.IGroupDAO;
import com.dykj.apps.web.sysmanager.dao.IRoleManagerDAO;
import com.dykj.apps.web.sysmanager.dao.IUserManagerDAO;
import com.dykj.apps.web.sysmanager.dao.po.SysAuth;
import com.dykj.apps.web.sysmanager.dao.po.SysGroup;
import com.dykj.apps.web.sysmanager.dao.po.SysRole;
import com.dykj.apps.web.sysmanager.dao.po.SysUser;
import com.dykj.apps.web.sysmanager.dto.GroupInfoDTO;
import com.dykj.apps.web.sysmanager.dto.SysUserDTO;
import com.dykj.platform.framework.common.paging.Page;
import com.dykj.platform.framework.exception.ApplicationException;
import com.dykj.platform.framework.exception.DatabaseException;
import com.dykj.platform.framework.utils.BeanUtils;

public class UserManagerBSImpl implements IUserManagerBS {

	private IUserManagerDAO dao;
	private IRoleManagerDAO roledao;
	private IAuthManagerDAO authdao;
	private IGroupDAO groupdao;
	/**
	 * 删除一个用户
	 */
	public int delUser(String key) throws ApplicationException {
		try {
			dao.delByKey(SysUser.class, key);
		} catch (DatabaseException e) {
			throw new ApplicationException(e);
		}
		return 0;
	}
	/**
	 *保存或修改用户
	 */
	public int saveOrUpdateUser(SysUserDTO dto) throws ApplicationException {
		try {
			String id = dto.getUserid();
			Object obj = dao.getObject(SysUser.class, id);
			if(null==obj){
				SysUser user = new SysUser();
				BeanUtils.copyProperties(user, dto);
				dao.save(user);
			}else{
				SysUser user = (SysUser)obj;
				BeanUtils.copyProperties(user, dto);
				dao.update(user);
			}
		} catch (DatabaseException e) {
			throw new ApplicationException(e);
		}
		return 0;
	}
	
	/**
	 *保存或修改用户  补录
	 */
	public int saveOrUpdateUserRenew(SysUserDTO dto) throws ApplicationException {
		try {
				SysUser user = new SysUser();
				BeanUtils.copyProperties(user, dto);
				dao.save(user);
		} catch (DatabaseException e) {
			throw new ApplicationException(e);
		}
		return 0;
	}	
	
	/**
	 *取得一个用户
	 */
	public SysUserDTO getOneSysUser(String id) throws ApplicationException {
		SysUserDTO dto = new SysUserDTO();
		try {
			Object obj = dao.getObject(SysUser.class, id);
			if(obj!=null){
				SysUser user = (SysUser)obj;
				BeanUtils.copyProperties(dto, user);
				if ("0".equals(dto.getRoleid())){
					dto.setRoleid("");
				}
			}else{
				return null;
			}
		} catch (DatabaseException e) {
			throw new ApplicationException(e);
		}
		return dto;
	}
	/**
	 * 翻页查询
	 */
	public List querySysUser(SysUserDTO dto, Page page) throws ApplicationException {
		List<SysUserDTO> list = new ArrayList<SysUserDTO>();
		try {
			List userList = dao.query(dto, page);
			for(int i=0;i<userList.size();i++){
				SysUserDTO userdto = new SysUserDTO();
				SysUser user = (SysUser)userList.get(i);
				BeanUtils.copyProperties(userdto, user);
				if ("0".equals(userdto.getRoleid())){
					userdto.setRoleid("");
				}
				list.add(userdto);
			}
		} catch (DatabaseException e) {
			throw new ApplicationException(e);
		}
		return list;
	}
	public List queryRoleUser(Long roleid,String groupid) throws ApplicationException {
		List list = new ArrayList();
		List<SysUserDTO> result = new ArrayList<SysUserDTO>();
		try {
			if(roleid==null){
				list = dao.queryNoRoleUser(groupid);
			}else{
				list = dao.queryHaveRoleUser(roleid,groupid);
			}
			for(int i=0;i<list.size();i++){
				SysUserDTO userdto = new SysUserDTO();
				SysUser user = (SysUser)list.get(i);
				BeanUtils.copyProperties(userdto, user);
				result.add(userdto);
			}
		} catch (DatabaseException e) {
			throw new ApplicationException(e);
		}
		return result;
	}
	/**
	 * from SysRole s 
	 * if(null!=roleid)
	 * hql = hql+" where s.creator ='"+roleid+"'";
	 */
	public Map queryAllRole(String creator) throws ApplicationException {
		Map<String, String> map = new LinkedHashMap<String, String>();
		try {
			map.put("", "请选择");
			List list =roledao.queryAllRole(creator);
			for(int i=0;i<list.size();i++){
				SysRole role = (SysRole)list.get(i);
				map.put(role.getRoleid().toString(), role.getRoledesc());
			}
		} catch (DatabaseException e) {
			throw new ApplicationException(e);
		}
		return map;
	}
	
	public List queryAuthUser(Long authid) throws ApplicationException {
		List list = new ArrayList();
		List<SysUserDTO> result = new ArrayList<SysUserDTO>();
		try {
			if(authid==null){
				list = dao.queryNoAuthUser();
			}else{
				list = dao.queryHaveAuthUser(authid);
			}
			for(int i=0;i<list.size();i++){
				SysUserDTO userdto = new SysUserDTO();
				SysUser user = (SysUser)list.get(i);
				BeanUtils.copyProperties(userdto, user);
				result.add(userdto);
			}
		} catch (DatabaseException e) {
			throw new ApplicationException(e);
		}
		return result;
	}
	
	public Map queryAllAuth(String authid) throws ApplicationException {
		Map<String, String> map = new LinkedHashMap<String, String>();
		try {
			map.put("0", "");
			List list =authdao.queryAllAuth(authid);
			for(int i=0;i<list.size();i++){
				SysAuth auth = (SysAuth)list.get(i);
				map.put(auth.getAuthid().toString(), auth.getAuthdesc());
			}
		} catch (DatabaseException e) {
			throw new ApplicationException(e);
		}
		return map;
	}
	public Map getGroupMap(String type, String groupid) throws ApplicationException {
		Map<String, String> groupMap = new LinkedHashMap<String, String>();
		groupMap.put("","请选择");
		try {
			List list = new ArrayList();
		if("0".equals(type)){
			list = groupdao.findGroupInfoById(null);
		}else{
			list = groupdao.findGroupInfoById(groupid);
		}
		for(int i=0;i<list.size();i++){				
			SysGroup gro = (SysGroup)list.get(i);
			GroupInfoDTO dto = new GroupInfoDTO();
			BeanUtils.copyProperties(dto,gro);
			groupMap.put(dto.getGroupid(), dto.getName()+"("+dto.getOrg()+", "+dto.getGlevel()+"级机构)");
		   }	
		}catch (DatabaseException e) {
			throw new ApplicationException(e);
		}
		return groupMap;
	}
	/*
	 * ================================get set方法=====================================
	 */	
	public IUserManagerDAO getDao() {
		return dao;
	}

	public void setDao(IUserManagerDAO dao) {
		this.dao = dao;
	}
	public IRoleManagerDAO getRoledao() {
		return roledao;
	}
	public void setRoledao(IRoleManagerDAO roledao) {
		this.roledao = roledao;
	}
	public IAuthManagerDAO getAuthdao() {
		return authdao;
	}
	public void setAuthdao(IAuthManagerDAO authdao) {
		this.authdao = authdao;
	}
	public IGroupDAO getGroupdao() {
		return groupdao;
	}
	public void setGroupdao(IGroupDAO groupdao) {
		this.groupdao = groupdao;
	}
	
}
